/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller.module;

import com.thienloc.omap.controller.BaseBean;
import com.thienloc.omap.util.Constant;
import com.thienloc.omap.util.JsfUtil;
import java.io.File;
import java.io.Serializable;
import java.util.Map;
import java.util.TreeMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.apache.commons.io.FileUtils;
//import org.primefaces.context.RequestContext;

/**
 *
 * @author GiangLT
 */
@ManagedBean
@ViewScoped
public class ConfigFileBean extends BaseBean implements Serializable {

    private Map<String, String> configFileMap;
    private String currentContent;
    private String selectedFilePath;
    private final Logger logger = LoggerFactory.getLogger(ConfigFileBean.class);

    public ConfigFileBean() {
    }

    @PostConstruct
    public void init() {
        try {
            File file = new File(Constant.CONFIG_FOLDER);
            File[] configFiles = file.listFiles();
            configFileMap = new TreeMap<String, String>();
            for (File configFile : configFiles) {
                configFileMap.put(configFile.getName(), configFile.getAbsolutePath());
            }
            this.currentContent = "";
            this.selectedFilePath = null;
        } catch (Exception ex) {
            String error = "Lỗi! ";
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage(error, ex.getMessage());
        }
    }

    public void viewFile() {
        try {
            currentContent = FileUtils.readFileToString(new File(selectedFilePath));
//            RequestContext.getCurrentInstance().scrollTo("fileContent");
        } catch (Exception ex) {
            String error = "Lỗi! ";
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage(error, ex.getMessage());
        }
    }

    public void saveFile() {
        try {
            File file = new File(selectedFilePath);
            FileUtils.writeStringToFile(file, currentContent);
            JsfUtil.addSuccessMessage("Thành công!", "File cấu hình đã được lưu.");
            logger.info("[{}]:  File saved", getUsername());
//            RequestContext.getCurrentInstance().scrollTo("header");
        } catch (Exception ex) {
            String error = "Lỗi! ";
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage(error, ex.getMessage());
        }
    }

    public void deleteFile() {
        try {
            File file = new File(selectedFilePath);
            file.delete();
            init();
            JsfUtil.addSuccessMessage("Thành công!", "File cấu hình đã được xóa.");
//            RequestContext.getCurrentInstance().scrollTo("header");
        } catch (Exception ex) {
            String error = "Lỗi! ";
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage(error, ex.getMessage());
        }
    }

    public void cancel() {
        try {
            this.currentContent = "";
//            RequestContext.getCurrentInstance().scrollTo("header");
        } catch (Exception ex) {
            String error = "Lỗi! ";
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage(error, ex.getMessage());
        }
    }

    public Map<String, String> getConfigFileMap() {
        return configFileMap;
    }

    public void setConfigFileMap(Map<String, String> configFileMap) {
        this.configFileMap = configFileMap;
    }

    public String getCurrentContent() {
        return currentContent;
    }

    public void setCurrentContent(String currentContent) {
        this.currentContent = currentContent;
    }

    public String getSelectedFilePath() {
        return selectedFilePath;
    }

    public void setSelectedFilePath(String selectedFilePath) {
        this.selectedFilePath = selectedFilePath;
    }

    public String getSelectedFileName() {
        return selectedFilePath.substring(selectedFilePath.lastIndexOf(File.separator) + 1);
    }
}
